package graphic;

import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import core.ProgramFileRelease;

public class ReleasesPanel extends JScrollPane {

	protected static ReleasesPanel RELEASESPANEL;
	
	JTable releaseTable;
	DefaultTableModel releaseTableModel;
	
	int fileId;
	
	HashMap<Integer, String> releaseNote;

	public ReleasesPanel()
	{
		this.releaseNote = new HashMap<Integer, String>();
		
		this.createTable();
	}
	
	protected void createTable()
	{
		this.releaseTableModel = new DefaultTableModel()
		{ 
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		
		this.releaseTableModel.setColumnIdentifiers(ReleasesPanel.getTableHeader());
		
		this.releaseTable = new JTable(this.releaseTableModel);
		this.releaseTable.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		this.releaseTable.setForeground(Color.black);
		this.releaseTable.setGridColor(Color.black);
				
		this.releaseTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);		
		this.releaseTable.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{	
			@Override
			public void valueChanged(ListSelectionEvent e)
			{
				int selectedRow = ReleasesPanel.this.releaseTable.getSelectedRow();
				int releaseNumber = (Integer)ReleasesPanel.this.releaseTable.getValueAt(selectedRow, 3);
				String note = ReleasesPanel.this.releaseNote.get(releaseNumber);
				
				ReleaseNotePanel.getIstance().setReleaseNote(note);
			}
		});
		
		super.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
		super.setViewportView(this.releaseTable);
	}
	
	public void populateTable(LinkedList<ProgramFileRelease> releases)
	{
		this.createTable();
		
		for (Iterator iterator = releases.iterator(); iterator.hasNext();)
		{
			System.out.println("RELEASES PANEL: AGGIUNGO RIGA");
			ProgramFileRelease programFileRelease = (ProgramFileRelease) iterator.next();	
			this.releaseTableModel.insertRow(this.releaseTable.getRowCount(), new Object[]{programFileRelease.getFileId(), programFileRelease.getAdded_by(), programFileRelease.getAdded_when(), programFileRelease.getNumber()});
			this.releaseNote.put(programFileRelease.getNumber(), programFileRelease.getNote());
		}
		
		super.setViewportView(this.releaseTable);
		super.revalidate();
		super.repaint();			
	}
	
	public void refreshTable(LinkedList<ProgramFileRelease> releases)
	{
		this.createTable();
		this.populateTable(releases);
	}
	
	protected static Vector<String> getTableHeader()
	{
		Vector<String> header = new Vector<String>();
		header.add("Id");
		header.add("Last edit by");
		header.add("Last edit in");
		header.add("Number");
		
		return header;
	}
	
	public int getSelectedReleaseNumber()
	{
		return (Integer)this.releaseTable.getValueAt(this.releaseTable.getSelectedRow(), 3);
	}

	public int getFileId() {
		return fileId;
	}

	public void setFileId(int fileId) {
		this.fileId = fileId;
	}

	public static ReleasesPanel getInstance()
	{
		if(RELEASESPANEL == null) RELEASESPANEL = new ReleasesPanel();
		return RELEASESPANEL;
	}
	
}
